#include "LinkQueue.h"
#include "LinkStack.h"
#include "MyString.h"
#include <stdlib.h>
#include <stdio.h>
#define MAXNUM 3

typedef struct Car
{
    mystring name;
    double arrivetime;
    double leavetime;
}Car;

Car *CreatCar(const  char *name,double arrivetime)
{
    Car *newcar=(Car*)malloc(sizeof(Car));
    if(newcar==NULL)
    {
        printf("create a new car error\n");
        return NULL;
    }
    Init_string(&newcar->name,name);
    newcar->arrivetime=arrivetime;
    newcar->leavetime=0;
}

void FreeCar(Car *leavecar)
{
    if(leavecar==NULL)
        return;
    FreeString(&leavecar->name);
    free(leavecar);
}
void Come_Car(LinkStack *CarStop,LQueue *pavement)
{
    char carname[100]={0};
    double arrivertime=0;
    printf("please input your car name:");
    scanf("%s",carname);
    printf("please input your car arrivetime:");
    scanf("%lf",&arrivertime);
    Car *newcar=CreatCar(carname,arrivertime);
    if(newcar ==NULL)
    {
        printf("create a new car error\n");
        return;
    }
    if(CarStop->stack.len>=MAXNUM)
    {
        QPush(pavement,newcar);
        printf("停车场已满，汽车%s停在了便道%d的第个位置\n",newcar->name.string,pavement->queue.len);
    }
    else
    {
        Push(CarStop,newcar);
        printf("停车场未满，汽车%s停在了停车场%d的第个位置\n",newcar->name.string,CarStop->stack.len);
    }

}
void Leave_Car(LinkStack *CarStop,LQueue *pavement)
{
    printf("请输入你要出停车场的车牌:");
    char temp[100]={0};
    scanf("%s",temp);
    LinkStack TempStop;
    Init_string1(carname,temp);
    InitLinkstack(&TempStop);
    int Num=CarStop->stack.len;
    double LeaveCarTime=0;
    while(IsEmpty(CarStop)==false)
    {
        Car *car=(Car*)*Pop(CarStop);
        if(car->name.isequal(&car->name,&carname)==true)
        {
            printf("请输入车辆离开的时间\n");
            scanf("%lf",&car->leavetime);
            LeaveCarTime=car->leavetime;
            printf("车辆%s在%lf时间到达停车场， 在%lf时间离开停车场，停车费用是%lf\n",car->name.string,car->arrivetime,car->leavetime,(car->leavetime-car->arrivetime)*5);
            FreeCar(car);
            break;
        }
        else
        {
            Push(&TempStop,car);
        }
    }
    while(IsEmpty(&TempStop)==false)
    {
        Push(CarStop,*Pop(&TempStop));
    }
    if(Num==CarStop->stack.len)
    {
        printf("未找到你要的车牌\n");
    }
    else
    {
        if(IsQEmpty(pavement)==false)
        {
            Car *car=(Car*)*QPop(pavement);
            car->arrivetime=LeaveCarTime;
            Push(CarStop,car);
            printf("车辆%s在%lf时间到达停车场%d的第个位置\n",car->name.string,car->arrivetime,CarStop->stack.len);
        }
    }
    
}

void DisplayStack(LinkStack *s)
{

    Node *TravelPoint= s->stack.head;
    while(TravelPoint!=NULL)
    {
        Car *car=(Car*)TravelPoint->data;
        printf("车辆:%s 到达时间:%lf\n",car->name.string,car->arrivetime);
        TravelPoint=TravelPoint->next;
    }
    printf("\n");

}

void DisplayQueue(LQueue *q)
{
    Node *TravelPoint= q->queue.head;
    while(TravelPoint!=NULL)
    {
        Car *car=(Car*)TravelPoint->data;
        printf("车辆:%s 到达时间:%lf\n",car->name.string,car->arrivetime);
        TravelPoint=TravelPoint->next;
    }
    printf("\n");
}
void main()
{
    LinkStack CarStop;
    InitLinkstack(&CarStop);
    LQueue pavement;
    InitLQueue(&pavement);
    int choice=0;
    while(1)
    {
        printf("******************CarStop*****************\n");
        printf("1.车辆进入停车场\n");
        printf("2.车辆离开停车场\n");
        printf("3.查询停车场状态\n");
        printf("4.查询便道状态\n");
        printf("5.退出停车场系统 \n");
        printf("请输入你的选择:");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                Come_Car(&CarStop,&pavement);
                break;
            case 2:
            printf("qqqq\n");
                Leave_Car(&CarStop,&pavement);
                break;
            case 3:
                DisplayStack(&CarStop);
                break;
            case 4:
            DisplayQueue(&pavement);
                break;
            case 5:
                printf("thank for using\n");
                Node *TravelPoint= CarStop.stack.head;
                while(TravelPoint!=NULL)
                {
                    Car *car=(Car*)TravelPoint->data;
                    FreeCar(car);
                    TravelPoint=TravelPoint->next;
                }
                FreeStack(&CarStop);

                TravelPoint= pavement.queue.head;
                while(TravelPoint!=NULL)
                {
                    Car *car=(Car*)TravelPoint->data;
                    FreeCar(car);
                    TravelPoint=TravelPoint->next;
                }
                FreeLQueue(&pavement);
                return;
            default:
                break;
        }
    }

}
